package com.modules.shiro.global;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;

import java.util.List;

/**
 * 自定义Realm
 */
public class MyRealm extends AuthorizingRealm {

    //@Autowired
    //private LoginMapper loginMapper;
//
//    @Autowired
//    private SysRoleMapper sysRoleMapper;

    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //System.out.println("==授权方法=="+principalCollection);
        //获取登录成功后的账户名 -- 根据账户名查询对应的账户id，
       // SysUser sysUser =  (SysUser)principalCollection.getPrimaryPrincipal();
//        Sys_User sys_user = sysUserMapper.findSysUserByUsername(username);
//        //RBAC基于角色的权限控制
//        //根据用户id查询对应角色信息
       // List<SysRole> roleList = loginMapper.findSysRoleByUserId(sysUser.getId());
//        //角色用Set来装，目的是去重，还有info对象中需要Set格式
//        Set<String> roleSets = new HashSet<>();
//        //获取角色ID
//        List<Integer> role_ids = new ArrayList<>();
//        for (Sys_Role role:roleList) {
//            roleSets.add(role.getTitle());
//            role_ids.add(role.getId());
//        }
//        //根据账户id查询对应的角色
//        //根据对应的角色id查询对应权限
//        Set<String> sys_menuSets = new HashSet<>();
//        List<Sys_Menu> sys_menus = sysRoleMapper.findMenuListByRoleId(role_ids);
//        for (Sys_Menu sm:sys_menus) {
//            sys_menuSets.add(sm.getTitle());
//        }
//        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
//        //根据账户查询数据库中对应账户的角色
//        info.setRoles(roleSets);
//        //根据账户查询数据库中对应账户的权限
//        info.setStringPermissions(sys_menuSets);
//        return info;
        return null;
    }

    //认证/登录验证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
       // System.out.println("======进入认证=====");
        //接收客户端的账户
        //String username =  authenticationToken.getPrincipal().toString();
        //SysUser sysUser = loginMapper.findSysUserByUsername(username);
        //if(sysUser != null){
           // SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(sysUser,sysUser.getPassword(),this.getName());
            //加盐
           // info.setCredentialsSalt(ByteSource.Util.bytes("gxa"));
           // return info;
       // }
        return new SimpleAuthenticationInfo("123","123",this.getName());
    }

}
